package cn.kgc.service.impl;


import cn.kgc.entity.MenuInfo;
import cn.kgc.entity.RoleInfo;
import cn.kgc.entity.RoleMenu;
import cn.kgc.mapper.MenuMapper;
import cn.kgc.mapper.RoleMapper;
import cn.kgc.mapper.RoleMenuMapper;
import cn.kgc.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private MenuMapper menuMapper;



    @Override
    public Page<List<RoleInfo>> getRole(Integer pageNo, Integer pageSize, String name) {
        PageHelper.startPage(pageNo,pageSize);
        Page<List<RoleInfo>> page = (Page) roleMapper.selectAll();
        return page;
    }

    @Override
    @Transactional
    public void addRole(RoleInfo roleInfo) {
        // 新增role
        System.out.println("前："+roleInfo);
        roleMapper.insert(roleInfo);
        System.out.println("后："+roleInfo);

        String auths = roleInfo.getAuths();
        if(auths != null){
            //  auths=1|0,2|1,3|1
            String[] arrAuths = auths.split(",");
            Set<String> menuIds = new HashSet<>();
            for (String auth: arrAuths){
                String[] str_auths = auth.split("\\|");
                System.out.println("str_auths: " + Arrays.asList(str_auths));
                menuIds.add(str_auths[0]);
                menuIds.add(str_auths[1]);
            }
            System.out.println("menuIds:  " + menuIds);
            for(String mid : menuIds){
                // 新增role_menu
                RoleMenu roleMenu =
                        new RoleMenu(null, roleInfo.getId(), Integer.parseInt(mid));
                roleMenuMapper.insert(roleMenu);
            }
        }
    }

    @Override
    public RoleInfo getRoleById(Integer id) {
        RoleInfo roleInfo = roleMapper.selectByPrimaryKey(id);
        List<String> authList = new ArrayList<>();
        // 根据角色id查角色菜单映射表，并且转成 ‘菜单id|父菜单id’
        List<RoleMenu> rmList = roleMenuMapper.select(new RoleMenu(null, id, null));
        for(RoleMenu rm : rmList){
            MenuInfo menuInfo = menuMapper.selectByPrimaryKey(rm.getMenuId());
            authList.add(rm.getMenuId() + "|" + menuInfo.getPid());
        }
        String auths = StringUtils.collectionToDelimitedString(authList, ",");
        System.out.println("auths: " + auths);
        roleInfo.setAuths(auths);

        return roleInfo;
    }

    @Override
    @Transactional
    public void updateRole(RoleInfo roleInfo) {
        // 修改role
        roleMapper.updateByPrimaryKey(roleInfo);

        // 删除该角色的所有菜单权限
        roleMenuMapper.delete(new RoleMenu(null,roleInfo.getId(),null));

        // 新增
        String auths = roleInfo.getAuths();
        if(auths != null){
            //  auths=1|0,2|1,3|1
            String[] arrAuths = auths.split(",");
            Set<String> menuIds = new HashSet<>();
            for (String auth: arrAuths){
                String[] str_auths = auth.split("\\|");
                System.out.println("str_auths: " + Arrays.asList(str_auths));
                menuIds.add(str_auths[0]);
                menuIds.add(str_auths[1]);
            }
            System.out.println("menuIds:  " + menuIds);
            for(String mid : menuIds){
                // 新增role_menu
                RoleMenu roleMenu =
                        new RoleMenu(null, roleInfo.getId(), Integer.parseInt(mid));
                roleMenuMapper.insert(roleMenu);
            }
        }
    }

//删除role
    @Override
    @Transactional
    public void deleteRole(String ids) {
        String[] idsArr = ids.split(",");
        for (String id : idsArr){
            roleMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<RoleInfo> getRoleById2() {
        return roleMapper.selectAll();
    }


}
